{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Morphological operations\n",
    "\n",
    "Morphology is the study of shapes. In image processing, some simple operations can get you a long way. The first things to learn are *erosion* and *dilation*. In erosion, we look at a pixel’s local neighborhood and replace the value of that pixel with the minimum value of that neighborhood. In dilation, we instead choose the maximum."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from matplotlib import pyplot as plt, cm\n",
    "import skdemo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "image = np.array([[0, 0, 0, 0, 0, 0, 0],\n",
    "                  [0, 0, 0, 0, 0, 0, 0],\n",
    "                  [0, 0, 1, 1, 1, 0, 0],\n",
    "                  [0, 0, 1, 1, 1, 0, 0],\n",
    "                  [0, 0, 1, 1, 1, 0, 0],\n",
    "                  [0, 0, 0, 0, 0, 0, 0],\n",
    "                  [0, 0, 0, 0, 0, 0, 0]], dtype=np.uint8)\n",
    "plt.imshow(image);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The documentation for scikit-image's morphology module is\n",
    "[here](http://scikit-image.org/docs/0.10.x/api/skimage.morphology.html).\n",
    "\n",
    "Importantly, we must use a *structuring element*, which defines the local\n",
    "neighborhood of each pixel. To get every neighbor (up, down, left, right, and\n",
    "diagonals), use `morphology.square`; to avoid diagonals, use\n",
    "`morphology.diamond`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from skimage import morphology\n",
    "sq = morphology.square(width=3)\n",
    "dia = morphology.diamond(radius=1)\n",
    "print(sq)\n",
    "print(dia)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The central value of the structuring element represents the pixel being considered, and the surrounding values are the neighbors: a 1 value means that pixel counts as a neighbor, while a 0 value does not. So:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "skdemo.imshow_all(image, morphology.erosion(image, sq))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "skdemo.imshow_all(image, morphology.dilation(image, sq))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "skdemo.imshow_all(image, morphology.dilation(image, dia))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Erosion and dilation can be combined into two slightly more sophisticated operations, *opening* and *closing*. Here's an example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "image = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
    "                  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
    "                  [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],\n",
    "                  [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],\n",
    "                  [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],\n",
    "                  [0, 0, 1, 1, 1, 0, 0, 1, 0, 0],\n",
    "                  [0, 0, 1, 1, 1, 0, 0, 1, 0, 0],\n",
    "                  [0, 0, 1, 1, 1, 0, 0, 1, 0, 0],\n",
    "                  [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],\n",
    "                  [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],\n",
    "                  [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],\n",
    "                  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
    "                  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], np.uint8)\n",
    "plt.imshow(image);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "What happens when run an erosion followed by a dilation of this image?\n",
    "\n",
    "What about the reverse?\n",
    "\n",
    "Try to imagine the operations in your head before trying them out below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "skdemo.imshow_all(image, morphology.opening(image, sq)) # erosion -> dilation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "skdemo.imshow_all(image, morphology.closing(image, sq)) # dilation -> erosion"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exercise**: use morphological operations to remove noise from a binary image."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from skimage import data, color\n",
    "hub = color.rgb2gray(data.hubble_deep_field()[350:450, 90:190])\n",
    "plt.imshow(hub);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Remove the smaller objects to retrieve the large galaxy."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
